<template>
  <div class="ele-body">
    <el-card shadow="never">
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)">发布新版本
          </el-button>
        </template>
        <template slot="action" slot-scope="{row}">
          <el-popconfirm
            class="ele-action"
            title="确定要删除此记录吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete">删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <publish v-if="showEdit" :data="current" :visible.sync="showEdit" @done="reload"/>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Publish from './publish'
export default {
  components: {Publish},
  computed: {
    ...mapGetters(['permission']),
  },
  data() {
    return {
      url: '/app_version/index',
      columns: [
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'version',
          label: '版本',
          align: 'center',
          showOverflowTooltip: true,
          width: 120
        },
        {
          prop: 'describe',
          label: '描述',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: 'create_time',
          label: '发布时间',
          align: 'center',
          showOverflowTooltip: true,
          width: 160,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue)
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 120,
          align: 'center',
          resizable: false,
          slot: 'action'
        }
      ],
      where: {},
      current: null,
      showEdit: false
    }
  },
  methods: {
    reload() {
      this.$refs.table.reload({where: this.where})
    },
    reset() {
      this.where = {}
      this.reload()
    },
    openEdit(row) {
      this.current = row
      this.showEdit = true
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true})
      this.$http.post('/app_version/delete', {id: row.id}).then(res => {
        loading.close()
        if (res.data.code === 0) {
          this.$message.success(res.data.msg)
          this.reload()
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(e => {
        loading.close()
        this.$message.error(e.message)
      })
    },
  }
}
</script>